package com.shaozi.nc.mq.visitor;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.shaozi.data.mq.business.bean.MQExchangeData;
import com.shaozi.data.mq.business.bean.MQExchangeRuleBean;
import com.shaozi.data.mq.business.bean.MQPushItem;
import com.shaozi.data.mq.business.bus.MqQuartzSchedule;
import com.shaozi.lib.throwable.SzException;
import com.shaozi.nc.data.model.MqApiQuartzModel;
import com.shaozi.nc.manage.secretary.SecretaryApiQuartzManage;

@Component
public class MqQuartzScheduleVisit implements MqQuartzSchedule {

	private Logger log = Logger.getLogger(MqQuartzScheduleVisit.class);
	@Autowired
	private SecretaryApiQuartzManage mgr;

	public MqQuartzScheduleVisit() {

	}

	@Override
	public void saveAndSchedule(MQExchangeData data, String jsonStr, String destName) throws SzException {
		MQExchangeRuleBean exchangeRule = (MQExchangeRuleBean) data.getRule();
		MQPushItem pushItem = exchangeRule.getPush();

		MqApiQuartzModel model = new MqApiQuartzModel();
		model.setAtTime(pushItem.getAtTime());
		model.setPushRegex(pushItem.getPushRegex());
		model.setTopicName(destName);
		model.setJsonData(jsonStr);
		model.setSourceId(pushItem.getSourceId());
		model.setSourceType(pushItem.getSourceType());
		model.setIsDebug(data.getIsDebug());
		model.setTriggerUserId(pushItem.getUserId());
		try {
			mgr.saveAndSchedule(data.getCompanyId(), model);
		} catch (SzException e) {
			log.error(SzException.getStackMessage(e));
		}

	}

	@Override
	public void removeSchedule(MQExchangeData data, int sourceType) throws SzException {
		MQExchangeRuleBean exchangeRule = (MQExchangeRuleBean) data.getRule();
		MQPushItem pushItem = exchangeRule.getPush();
		mgr.removeSchedule(data.getCompanyId(), pushItem.getSourceId(), sourceType, pushItem.getUserId());

	}

}
